package app.nexd.com.indoor.presenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import app.nexd.com.indoor.activity.inter.INexdMapViewActivity;
import app.nexd.com.indoor.application.MyApplication;
import app.nexd.com.indoor.mode.FloorModel;
import app.nexd.com.indoor.mode.MallModel;
import app.nexd.com.indoor.presenter.inter.INexdMapViewPresenter;
import app.nexd.com.indoor.util.DateUtil;
import app.nexd.com.indoor.util.FileUtil;
import app.nexd.com.indoor.util.ProgressDialogUtils;
import app.nexd.com.indoor.util.UrlUtil;
import cn.nexd.map.NexdLocationAgent;
import cn.nexd.map.NexdLocationListener;
import cn.nexd.map.location.utils.Log;
import cn.nexd.map.rendering.core.componet.SVGMapView;
import cn.nexd.map.rendering.overlay.SVGMapLocationOverlay;

/**
 * Created by lawrence on 2015/12/4.
 */
public class INexdMapViewPresenterImpl implements INexdMapViewPresenter {

    private NexdLocationAgent nexdLocationAgent;

    private SVGMapView nexdMap;

    private FloorModel floorModel;

    private Map<String, List<FloorModel>> floorModels;

    private Context context;

    private MallModel mallModel;

    private INexdMapViewActivity iNexdMapViewActivity;

    private String mallpath = "";

    private SVGMapLocationOverlay svgMapLocationOverlay;

    public INexdMapViewPresenterImpl(SVGMapView nexdMap, Context context, MallModel mallModel, INexdMapViewActivity iNexdMapViewActivity) {
        this.context = context;
        this.nexdMap = nexdMap;
        this.mallModel = mallModel;
        this.iNexdMapViewActivity = iNexdMapViewActivity;
        this.mallpath = UrlUtil.BASE_FILE_PATH + File.separator + mallModel.getId() + File.separator;
    }

    @Override
    public void setCurrentFloor(FloorModel floorModel) {
        ProgressDialogUtils.showProgressDialog(context, "切换中...", "", true);
        this.floorModel = floorModel;
        stopLocation();
        String svgPath = getSvgPath(floorModel.getFloorSvg());
        android.util.Log.d("切换楼层", svgPath);
        if (checkFileExist(svgPath)) {
            iNexdMapViewActivity.setCurrentFloor(svgPath);
        } else {
            iNexdMapViewActivity.showToast("未找到当前楼层信息");
        }
    }

    // 设置定位结果监听
    private void setLocationListener() {
        nexdLocationAgent.setLocationListener(new NexdLocationListener() {
            @Override
            public void onLocationStart() {

            }

            @Override
            public void onLocationSuccess(PointF pointF, String floorId) {
                Log.d("LocationListener", "定位成功");
                if (svgMapLocationOverlay == null) {
                    svgMapLocationOverlay = new SVGMapLocationOverlay(nexdMap);
                }
                if (nexdMap.isMapLoadFinsh()) {
                    svgMapLocationOverlay.setPosition(pointF);
                    nexdMap.addOverlay(svgMapLocationOverlay, 0);
                    iNexdMapViewActivity.refreshNexdMap();
                }
            }

            @Override
            public void onLocationFailed(String message) {
                Log.d("LocationListener", "定位失败");
                iNexdMapViewActivity.showToast("定位失败");
            }
        });
    }

    @Override
    public void loadMap(FloorModel floorModel) {
        this.floorModel = floorModel;
        Log.d("svgPath", getSvgPath(floorModel.getFloorSvg()));
        if (checkFileExist(getSvgPath(floorModel.getFloorSvg()))) {
            iNexdMapViewActivity.loadMap(getSvgPath(floorModel.getFloorSvg()));
        } else {
            iNexdMapViewActivity.showToast("未找到当前楼层信息");
        }
    }

    @Override
    public void saveNexdMap(Bitmap bitmap) {
        String fileName = UrlUtil.BASE_FILE_PATH + File.separator + "img" + File.separator + DateUtil.getCurrentByType("yyyyMMDDHHmmss") + ".png";
        File file = FileUtil.buildFile(fileName, false);
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            MyApplication.getInstance().reportException(e);
        } catch (IOException e) {
            MyApplication.getInstance().reportException(e);
        }
    }

    /*
     * 检查文件是否存在， 用来判断是否确实文件
     */
    private boolean checkFileExist(String svgPath) {
        if (FileUtil.checkExist(svgPath)) {
            return true;
        } else {
            return false;
        }
    }

    // 获取svg文件路径
    private String getSvgPath(String svgName) {
        return mallpath + "SVG" + File.separator + svgName;
    }

    @Override
    public void initMap() {
        nexdMap.setMaxZoomValue(300);
        nexdLocationAgent = NexdLocationAgent.getLocationAgent();
        nexdLocationAgent.setContext(context);
        nexdLocationAgent.setShowTextSize(275);
        nexdLocationAgent.setIsHightLight(false);
        nexdLocationAgent.setTextSize(30);
    }

    @Override
    public void setLocationConfig() {
        nexdLocationAgent.setLocationConfig(this.mallpath + File.separator + mallModel.getId() + File.separator, floorModel.getFloorId());
        setLocationListener();
    }

    @Override
    public void startLocation() {
        nexdLocationAgent.startLocation(1500);
    }

    @Override
    public void stopLocation() {
        nexdLocationAgent.stopLocation();
    }
}
